
#include "../main.h"


/*
 * Define a bunch of properties with a JSPropertySpec array statically
 * initialized and terminated with a null-name entry.  Besides its name,
 * each property has a "tiny" identifier (MY_COLOR, e.g.) that can be used
 * in switch statements (in a common my_getProperty function, for example).
 */
enum test_tinyid {
	TEST_A, TEST_B, TEST_C, TEST_D, TEST_E, TEST_F
};


/*
 * Given the above definitions and call to JS_DefineProperties, obj will
 * need this sort of "getter" method in its class (my_class, above).  See
 * the example for the "It" class in js.c.
 */

JSBool
//getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
getProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp) {
    //if (JSVAL_IS_INT(id)) {
        //switch (JSVAL_TO_INT(id)) {
        switch (id) {
          case TEST_A:  *vp = INT_TO_JSVAL(1); break;
          case TEST_B:  *vp = INT_TO_JSVAL(2); break;
          case TEST_C:  *vp = INT_TO_JSVAL(3); break;
          case TEST_D:  *vp = INT_TO_JSVAL(4); break;
          case TEST_E:  *vp = INT_TO_JSVAL(5); break;
          case TEST_F:  *vp = INT_TO_JSVAL(6); break;
        }
    //}
    return JS_TRUE;
}


//////////////////////////////////////////////////////////////////////////////
//
static JSBool
Test_Hello (JSContext *cx, uintN argc, jsval *vp) {
	printf("Hello, world!\n");
}



static JSClass __class = {
    "Test", 0,
    JS_PropertyStub, JS_PropertyStub, getProperty,

#if JS_VERSION > 180
    JS_StrictPropertyStub,
#else
    JS_PropertyStub,
#endif
    
    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
    JSCLASS_NO_OPTIONAL_MEMBERS
};


static JSPropertySpec props[] = {
    {"a",      TEST_A,      JSPROP_ENUMERATE},
    {"b",      TEST_B,      JSPROP_ENUMERATE},
    {"c",      TEST_C,      JSPROP_ENUMERATE},
    {"d",      TEST_D,      JSPROP_ENUMERATE},
    {"e",      TEST_E,      JSPROP_ENUMERATE},
    {"f",      TEST_F,      JSPROP_READONLY},
    {0}
};

static JSFunctionSpec methods[] = {
	JS_FN("hello",  Test_Hello, 2,0),
	JS_FS_END
};



//////////////////////////////////////////////////////////////////////////////
//
void class_test_init(JSContext *cx){
	JSObject *obj = JS_DefineObject(cx, JS_GetGlobalObject(cx),
		"test",
		&__class, NULL, JSPROP_ENUMERATE);
	JS_DefineProperties(cx, obj, props);
	JS_DefineFunctions(cx, obj, methods);
}




